package com.yang.servlet;

import com.yang.pojo.BasicInfo;
import com.yang.pojo.Brand;
import com.yang.service.BasicInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/BasicUpdateServlet")
public class BasicUpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BasicInfoService basicInfoService = new BasicInfoService();
        int basicInfoId = Integer.parseInt(request.getParameter("basicInfoId"));
        String basicInfoName = request.getParameter("basicInfoName");

        try {
            boolean b = basicInfoService.updateBasicInfoById(basicInfoName, basicInfoId);
            String tips = b?"<label style='color:green'>修改成功</label>":"<label style='color:red'>修改失败</label>";
            request.setAttribute("tips",tips);
            ArrayList<BasicInfo> basicInfo = basicInfoService.findBasicInfo();
            request.setAttribute("basicInfo",basicInfo);
            request.getRequestDispatcher("basic_info_list.jsp").forward(request,response);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
